<?php
namespace net\yun2;


/**
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 *
 * Center-Controller-Service 中控服务PHP客户端
 * @copyright by yun2.net
 */
class CCS
{
    const SECRET_KEY = '此处填写第三方平台中控服务参数加密secret';
    

    private static function _curl($url, $method = 'POST', $postJson = '')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        if ($postJson) {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $postJson);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                    "Content-Type: application/json",
                    "Content-Length: " . strlen($postJson))
            );
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 90);

        $response = curl_exec($curl);

        curl_close($curl);

        //请求数据
        return json_decode($response, true);
    }

    public static function requestApi($params, $key = '')
    {
        $url = isset($params['url']) ? $params['url'] : '';
        $endpoint = isset($params['endpoint']) ? $params['endpoint'] : '';
        $componentAppId = isset($params['componentAppId']) ? $params['componentAppId'] : '';
        $appId = isset($params['appId']) ? $params['appId'] : '';
        $method = isset($params['method']) ? strtoupper($params['method']) : '';
        $query = isset($params['query']) ? $params['query'] : '';
        $post = isset($params['post']) ? $params['post'] : '';

        $key = $key ? $key : self::SECRET_KEY;

        if (!$key) die('没有配置参数加密字串');
        if (!$endpoint) die('没有设置微信API地址');
        if (!$url) die('缺少参数URL');
        if (!$componentAppId) die('缺少第三方平台AppID');
        if (!$appId) die('缺少授权给第三方平台的公众号/小程序AppID');
        if (in_array($method, ['POST', 'GET']) == false) die('请求method无效');

        $data = compact("endpoint", "componentAppId", "appId", "method");

        //设置GET参数
        $query && count(array_keys($query)) && $data['query'] = $query;

        //设置POST数据
        $method == 'POST' && $post && count(array_keys($post)) && ($data['post'] = $post);

        //将数据格式化为JSON字符串
        $postJson = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        //拼合key和JSON字符串
        $signString = $key . $postJson;

        //生成拼合字符串的md5值
        $sign = md5($signString);

        //将md5值作为URL GET 参数 sign
        $url = "{$url}/index?sign={$sign}";

        return self::_curl($url, 'POST', $postJson);
    }

    public static function getToken($params, $key = '')
    {
        $url = isset($params['url']) ? $params['url'] : '';
        $componentAppId = isset($params['componentAppId']) ? $params['componentAppId'] : '';
        $appId = isset($params['appId']) ? $params['appId'] : '';

        $key = $key ? $key : self::SECRET_KEY;

        if (!$key) die('没有配置参数加密字串');
        if (!$url) die('缺少参数URL');
        if (!$componentAppId) die('缺少第三方平台AppID');

        $data = compact("componentAppId");
        if ($appId) $data['appId'] = $appId;

        //将数据格式化为JSON字符串
        $postJson = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        //拼合key和JSON字符串
        $signString = $key . $postJson;

        //生成拼合字符串的md5值
        $sign = md5($signString);

        //将md5值作为URL GET 参数 sign

        $url = "{$url}/getToken?sign={$sign}";

        //请求数据
        return self::_curl($url, 'POST', $postJson);
    }

    public static function updateToken($params, $key = '')
    {
        $url = isset($params['url']) ? $params['url'] : '';
        $componentAppId = isset($params['componentAppId']) ? $params['componentAppId'] : '';
        $appId = isset($params['appId']) ? $params['appId'] : '';

        $key = $key ? $key : self::SECRET_KEY;

        if (!$key) die('没有配置参数加密字串');
        if (!$url) die('缺少参数URL');
        if (!$componentAppId) die('缺少第三方平台AppID');


        $data = compact("componentAppId");
        if ($appId) $data['appId'] = $appId;

        //将数据格式化为JSON字符串
        $postJson = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        //拼合key和JSON字符串
        $signString = $key . $postJson;

        //生成拼合字符串的md5值
        $sign = md5($signString);

        //将md5值作为URL GET 参数 sign

        $url = "{$url}/updateToken?sign={$sign}";

        //请求数据
        return self::_curl($url, 'POST', $postJson);
    }
}